package com.example.demo.controller;


import com.example.demo.entity.Book;
import com.example.demo.entity.Category;
import com.example.demo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/book")
@CrossOrigin(origins="http://localhost:8080")

public class BookController {
    @Autowired
    BookService bookService;

    @GetMapping("/all")
    public Object getAll() {

        return bookService.list();
    }

    @GetMapping("/getbook_categroy")
    public Object getbook_categroy(Integer category) {
        List<Book> list = bookService.list();
        List<Book> list2 = new ArrayList();
        for (Book c : list) {
            if (c.getCategory() == category) {
                list2.add(c);
            }
        }

        return list2;

    }

    @GetMapping("/getbook_id")
    public Object getbook_id(Integer id) {
        List<Book> list = bookService.list();

        for (Book c : list) {
            if (c.getId() == id) {

                return c;
            }
        }
        return list;
    }

   @PostMapping ("/getbook_name")
   public Object getbook_name(@RequestBody Book t)
    {
       List<Book> list =bookService.list();
       List<Book> list2 = new ArrayList();
        String search=t.getName();
        for(Book c:list) {
            String book=c.getName();
            if(search.length()>book.length())continue;
            else {
                if (search.equals(c.getName_leath(search.length()))){
                    list2.add(c);
                }
            }
        }
     return list2;
    }
}
